package com.imooc.pan.server.modules.file.converter;

import com.imooc.pan.server.modules.file.context.*;
import com.imooc.pan.server.modules.file.entity.RPanUserFile;
import com.imooc.pan.server.modules.file.po.*;
import com.imooc.pan.server.modules.file.vo.FileSearchResultVO;
import com.imooc.pan.server.modules.file.vo.FolderTreeNodeVO;
import com.imooc.pan.storage.engine.core.context.StoreChunkFileContext;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

/**
 * @author hhb
 * @data 2024/6/8 16:27
 */
@Mapper(componentModel = "spring")
public interface FileConverter {

    /**
     * CreateFolderPO转化为CreateFolderContext
     * 需要对加密的parentId进行解密
     * @param createFolderPO
     * @return
     */
    @Mapping(target = "parentId", expression = "java(com.imooc.pan.core.utils.IdUtil.decrypt(createFolderPO.getParentId()))")
    @Mapping(target = "userId", expression = "java(com.imooc.pan.server.common.utils.UserIdUtil.get())")
    CreateFolderContext createFolderPOToCreateFolderContext(CreateFolderPO createFolderPO);

    /**
     * UpdateFilenamePO转化为UpdateFilenameContext
     * @param updateFilenamePO
     * @return
     */
    @Mapping(target = "fileId", expression = "java(com.imooc.pan.core.utils.IdUtil.decrypt(updateFilenamePO.getFileId()))")
    @Mapping(target = "userId", expression = "java(com.imooc.pan.server.common.utils.UserIdUtil.get())")
    UpdateFilenameContext updateFilenamePOToUpdateFilenameContext(UpdateFilenamePO updateFilenamePO);


    /**
     * DeleteFilePO转化为DeleteFileContext
     * @param deleteFilePO
     * @return
     */
    @Mapping(target = "userId", expression = "java(com.imooc.pan.server.common.utils.UserIdUtil.get())")
    DeleteFileContext deleteFilePOToDeleteFileContext(DeleteFilePO deleteFilePO);

    /**
     * SecUploadFilePO转化为SecUploadFileContext
     * @param secUploadFilePO
     * @return
     */
    @Mapping(target = "parentId", expression = "java(com.imooc.pan.core.utils.IdUtil.decrypt(secUploadFilePO.getParentId()))")
    @Mapping(target = "userId", expression = "java(com.imooc.pan.server.common.utils.UserIdUtil.get())")
    SecUploadFileContext secUploadFilePOToSecUploadFileContext(SecUploadFilePO secUploadFilePO);

    /**
     * FileUploadPO转化为FileUploadContext
     * @param fileUploadPO
     * @return
     */
    @Mapping(target = "parentId", expression = "java(com.imooc.pan.core.utils.IdUtil.decrypt(fileUploadPO.getParentId()))")
    @Mapping(target = "userId", expression = "java(com.imooc.pan.server.common.utils.UserIdUtil.get())")
    FileUploadContext fileUploadPOToFileUploadContext(FileUploadPO fileUploadPO);

    /**
     * FileUploadContext转化为FileSaveContext
     * @param fileContext
     * @return
     */
    @Mapping(target = "record",ignore = true)
    FileSaveContext fileUploadCOntextToFileSaveContext(FileUploadContext fileContext);

    /**
     * FileChunkUploadPO转化为FileChunkUploadContext
     * @param fileChunkUploadPO
     * @return
     */
    @Mapping(target = "userId", expression = "java(com.imooc.pan.server.common.utils.UserIdUtil.get())")
    FileChunkUploadContext fileChunkUploadPOToFileChunkUploadContext(FileChunkUploadPO fileChunkUploadPO);

    /**
     * FileChunkUploadContext转换为FileChunkSaveContext
     * @param fileChunkUploadContext
     * @return
     */
    FileChunkSaveContext fileChunkUploadContextToFileChunkSaveContext(FileChunkUploadContext fileChunkUploadContext);

    /**
     * FileChunkSaveContext转化为StoreChunkFileContext
     * @param context
     * @return
     */
    @Mapping(target = "realPath",ignore = true)
    StoreChunkFileContext fileChunkSaveContextToStoreChunkFileContext(FileChunkSaveContext context);

    /**
     * QueryUploadChunksPO转化为QueryUploadChunksContext
     * @param queryUploadChunksPO
     * @return
     */
    @Mapping(target = "userId", expression = "java(com.imooc.pan.server.common.utils.UserIdUtil.get())")
    QueryUploadChunksContext queryUploadChunksPOToQueryUploadChunksContext(QueryUploadChunksPO queryUploadChunksPO);

    /**
     * FileChunkMergePo转化为FileChunkMergeContext
     * @param fileChunkMergePo
     * @return
     */
    @Mapping(target = "parentId", expression = "java(com.imooc.pan.core.utils.IdUtil.decrypt(fileChunkMergePo.getParentId()))")
    @Mapping(target = "userId", expression = "java(com.imooc.pan.server.common.utils.UserIdUtil.get())")
    FileChunkMergeContext fileChunkMergePoToFileChunkMergeContext(FileChunkMergePo fileChunkMergePo);

    /**
     * FileChunkMergeContext转化为FileChunkMergeAndSaveContext
     * @param context
     * @return
     */
    FileChunkMergeAndSaveContext fileChunkMergeContextToFileChunkMergeAndSaveContext(FileChunkMergeContext context);

    @Mapping(target = "label",source = "record.filename")
    @Mapping(target = "id",source = "record.fileId")
    @Mapping(target = "children", expression = "java(com.google.common.collect.Lists.newArrayList())")
    FolderTreeNodeVO rPanUserFileToFolderTreeNodeVO(RPanUserFile record);

    FileSearchResultVO rPanUserFileToFileSearchResultVO(RPanUserFile rPanUserFile);
}
